/*  
  lex analizer
  lex.l
  Scanner

  Created by Pei Liu on 10/5/11.
  Copyright 2011 __FOR CS440 COMPILER__. All rights reserved.
*/

%{
    #include <stdio.h>
    #include "stdlib.h"
    #include "hashtable.h"
    #include "pvm_parser.tab.h"
    #include "error.h"
    
    /*string list*/
    char** strlist;
    int strNbr = 0;
    /*line number*/
    int lineNbr = 1;
    
%}

%%
"and"   { return ANDTOKEN;}
"array" { return ARRAYTOKEN;}
"do"	{ return DOTOKEN;}
"else"  { return ELSETOKEN;}
"endo"  { return ENDOTOKEN;}
"endif" { return ENDIFTOKEN;}
"endproc"    { return ENDPROCTOKEN;}
"endwhile"   { return ENDWHILETOKEN;}
"if"    { return IFTOKEN;}
"line"  { return LINETOKEN;}
"not"   { return NOTTOKEN;}
"or"    { return ORTOKEN;}
"proc"  { return PROCTOKEN;}
"read"  { return READTOKEN;}
"readstr"   { return READSTRTOKEN;}
"retval"    { return RETVALTOKEN;}
"var"   { return VARTOKEN;}
"while" { return WHILETOKEN;}
"writestr"  { return WRITESTRTOKEN;}
"write" { return WRITETOKEN;}


"+" { return ADDTOKEN;}
"-" { return MINUSTOKEN;}
"*" { return MULTOKEN;}
"/" { return DIVTOKEN;}
"=" { return EQUTOKEN;}
"<" { return LESSTOKEN;}
"<=" { return LEQUTOKEN;}
">=" { return GEQUTOKEN;}
">" { return GRETOKEN;}
"#" { return NEQUTOKEN;}
"(" { return LPTOKEN;}
")" { return RPTOKEN;}
"[" { return LSPTOKEN;}
"]" { return RSPTOKEN;}
"," { return COMTOKEN;}
";" { return SCOMTOKEN;}

[_a-zA-Z][_a-zA-Z0-9]* {//get id name out, using strdup
						yylval.strval = strdup(yytext);
						return IDTOKEN;
						}

[|-]?[0-9]+ { yylval.intval = atoi(yytext); return INTTOKEN;}

"\""[^"]*"\"" { char* temp = malloc(strlen(yytext) * sizeof(char));
                int i = 1;
                int j = strlen(yytext) - 2;
                int k = 0;
                for(k = i; k <= j; k++)
                {
                    temp[k-i] = yytext[k];
                }
                temp[j-i+1] = 0;
                //strlist[strNbr] = temp;
                strNbr++;
                yylval.strval = temp;
                return STRTOKEN;
            }

"'"[^']{1}"'" { yylval.intval = (int)yytext[1]; return CHRTOKEN;}
    
\n  { lineNbr++;}
    
"//"[^\n]* {;}
    
[ \t]   {/*ignore*/}
    
.   { fprintf(stderr, LEX_ERR, lineNbr, yytext); exit(0);}
%%
